package com.example.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.dao.FileNodeRepository;
import com.example.entity.FileNode;
import com.example.service.FileNodeService;
import com.example.service.FileService;
import com.example.utils.ConstantVariable;
import com.example.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

/**
 *
 */
@Service
public class FileNodeServiceImpl implements FileNodeService {
    @Autowired
    FileNodeRepository fileNodeRepository;
    @Autowired
    FileService fileService;
    @Override
    public JSONObject insert(MultipartFile uploadFile, FileNode fileNode) {
        JSONObject jsonObject = new JSONObject();
        String realPath = ConstantVariable.baseDir;
        String scene = fileNode.getScene();
        realPath = realPath + "/" + scene;

        // 调用文件上传服务
        String filePath = fileService.upload(uploadFile, realPath);

        fileNode.setFilePath(filePath);

        fileNode.setOriginFileName(uploadFile.getOriginalFilename());
        String newFileName = filePath.substring(filePath.lastIndexOf("/")+1);
        fileNode.setNewFileName(newFileName);
        String uuid = UUID.randomUUID().toString();
        fileNode.setLogic_id(uuid);
        fileNode.setHasParent(false);
        fileNode.setHasChild(false);
        FileNode newFileNode = fileNodeRepository.save(fileNode);
        jsonObject.put("msg", "success");
        jsonObject.put("data", newFileNode);
        return jsonObject;
    }

    @Override
    public JSONObject update(MultipartFile uploadFile, FileNode f) {
        JSONObject jsonObject = new JSONObject();
        Optional<FileNode> fileNodeOptional = fileNodeRepository.findById(f.getId());
        FileNode fileNode = fileNodeOptional.get();
        fileNode.setDescription(f.getDescription());
        // 删除原来的文件
        String oldFilePath = fileNode.getFilePath();
        FileUtil.delete(oldFilePath);
        // 上传新来的文件
        String realPath = ConstantVariable.baseDir;
        String scene = fileNode.getScene();
        realPath = realPath + "/" + scene;
        // 调用文件上传服务
        String filePath = fileService.upload(uploadFile, realPath);
        // 为文件节点更新新的文件路径
        fileNode.setFilePath(filePath);

        fileNode.setOriginFileName(uploadFile.getOriginalFilename());
        String newFileName = filePath.substring(filePath.lastIndexOf("/")+1);
        fileNode.setNewFileName(newFileName);
        fileNode = fileNodeRepository.save(fileNode);
        jsonObject.put("msg", "success");
        jsonObject.put("data", fileNode);
        return jsonObject;
    }

    @Override
    public JSONObject deleteById(FileNode f) {
        JSONObject jsonObject = new JSONObject();
        Optional<FileNode> fileNodeOptional = fileNodeRepository.findById(f.getId());
        FileNode fileNode = fileNodeOptional.get();
        String filePath = fileNode.getFilePath();
        FileUtil.delete(filePath);
        fileNodeRepository.delete(fileNode);
        jsonObject.put("msg", "success");
        jsonObject.put("data", fileNode);
        return jsonObject;
    }

    @Override
    public JSONObject selectAll(FileNode fileNode) {
        JSONObject jsonObject = new JSONObject();
        Example<FileNode> example = Example.of(fileNode);
        List<FileNode> fileNodeList = fileNodeRepository.findAll(example);
        jsonObject.put("msg", "success");
        jsonObject.put("data", fileNodeList);
        return jsonObject;
    }

    @Override
    public JSONObject selectOneById(Long id) {
        JSONObject jsonObject = new JSONObject();
        Optional<FileNode> fileNodeOptional = fileNodeRepository.findById(id);
        FileNode fileNode = fileNodeOptional.get();
        jsonObject.put("msg", "success");
        jsonObject.put("data", fileNode);
        return jsonObject;
    }

    @Override
    public JSONObject updateProperty(FileNode f) {
        JSONObject jsonObject = new JSONObject();
        Optional<FileNode> fileNodeOptional = fileNodeRepository.findById(f.getId());
        FileNode fileNode = fileNodeOptional.get();
        fileNode.setDescription(f.getDescription());
        fileNode = fileNodeRepository.save(fileNode);
        jsonObject.put("msg", "success");
        jsonObject.put("data", fileNode);
        return jsonObject;
    }

    @Override
    public JSONObject deleteFile(FileNode f) {
        JSONObject jsonObject = new JSONObject();
        Optional<FileNode> fileNodeOptional = fileNodeRepository.findById(f.getId());
        FileNode fileNode = fileNodeOptional.get();
        String filePath = fileNode.getFilePath();
        FileUtil.delete(filePath);
        fileNode.setFilePath(null);
        fileNode.setNewFileName(null);
        fileNode.setOriginFileName(null);
        fileNode = fileNodeRepository.save(fileNode);
        jsonObject.put("msg", "success");
        jsonObject.put("data", fileNode);
        return jsonObject;
    }
}
